//
//  ClaimPicViewController.m
//  MemberProject
//
//  Created by vincent luo on 15/7/17.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "ClaimPicViewController.h"
#import "ClaimPicTableViewCell.h"
#import "ClaimPicWindow.h"
#import "ClaimPerInfoViewController.h"
@interface ClaimPicViewController ()<UITableViewDataSource,UITableViewDelegate,ClaimPicTableViewCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
@property (strong, nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSArray *data;
@property(strong,nonatomic)UIImagePickerController *imagePicker;
@property(strong,nonatomic)UIImagePickerController *cameraPicker;
@property(strong,nonatomic)ClaimPicWindow *picWindow;
@property(assign,nonatomic)int tag;
@end

@implementation ClaimPicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"自助申请";

    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    
    self.data=@[@"医疗费用原始凭证(必填)",@"被保险人身份证明(必填)",@"门(急)诊病例(必填)",@"住院病例(出院小结)",@"医疗费用明细清单/处方",@"病理/血液/影像检查报告",@"意外事故证明"];
    
    for(int i=0;i<self.data.count;i++){
        NSString *CellIdentifier=[[NSString alloc]initWithFormat:@"CellIdentifier%d",i];
        [self.tableView registerNib:[UINib nibWithNibName:@"ClaimPicTableViewCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    }
    
    
    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(5, (footerView.height-40)/2, SCREEN_WIDTH-2*5, 40);
    [footerView addSubview:button];
    [button setTitle:@"下一步" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"redbar.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickNextStep:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView=footerView;
}

-(void)chickNextStep:(UIButton *)sender{
    NSMutableArray *arrM=[NSMutableArray array];

    ClaimPicTableViewCell *cell=[[self.tableView visibleCells]lastObject];
    UIView *superview=cell.superview;
    if(superview){
        for(int i=0;i<superview.subviews.count;i++){
            ClaimPicTableViewCell *cell=(ClaimPicTableViewCell*)[superview viewWithTag:1000+i];
            [arrM addObject:cell.arrM];
        }
    }
    if(self.isUpdate==YES){
        UserClaimInput *claimInput=[UserInfoManager sharedManager].userClaimInput;
        claimInput.images=arrM;
        if(self.isUpdate==YES){
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            ClaimPerInfoViewController *vc=[[ClaimPerInfoViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        if(((NSArray *)arrM[0]).count==0||((NSArray *)arrM[1]).count==0||((NSArray *)arrM[2]).count==0){
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"必填项不能为空!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
            
        }else{
            UserClaimInput *claimInput=[UserInfoManager sharedManager].userClaimInput;
            claimInput.images=arrM;
            if(self.isUpdate==YES){
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                ClaimPerInfoViewController *vc=[[ClaimPerInfoViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }
    }
    
    
    
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifer=[[NSString alloc]initWithFormat:@"CellIdentifier%d",indexPath.section];
    ClaimPicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.tag=1000+indexPath.section;
    cell.delegate=self;
    if(cell.arrM==nil){
        if(self.isUpdate){
            cell.arrM=[[NSMutableArray alloc]initWithArray:self.images[indexPath.section]];
        }else{
            cell.arrM=[NSMutableArray array];
        }
        [cell.collection reloadData];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 93+20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.width, 25)];
    view.backgroundColor=[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, view.bounds.size.width-20, view.bounds.size.height)];
    label.text=self.data[section];
    label.font=[UIFont boldSystemFontOfSize:13.0f];
    [view addSubview:label];
    return view;
}

#pragma mark - ClaimPicTableViewCell
-(void)didSelectImage:(UIImage *)image{
    if(self.picWindow==nil){
        self.picWindow=[[ClaimPicWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    [self.picWindow showWithImage:image];
}

-(void)chickAddButtonWithCell:(ClaimPicTableViewCell *)cell{
    NSIndexPath *indexPath=[self.tableView indexPathForCell:cell];
    self.tag=indexPath.section;
//    self.data=@[@"医疗费用原始凭证(必填)",@"被保险人身份证明(必填)",@"门(急)诊病例(必填)",@"住院病例(出院小结)",@"医疗费用明细清单/处方",@"病理/血液/影像检查报告",@"意外事故证明"];

    switch (indexPath.section) {
        case 0:{
            //医疗费用原始凭证(必填)
        }
            break;
        case 1:{
            //被保险人身份证明(必填)
        }
            break;
        case 2:{
            //门(急)诊病例(必填)
        }
            break;
        case 3:{
            //住院病例(出院小结)
        }
            break;
        case 4:{
            ///医疗费用明细清单/处方
        }
            break;
        case 5:{
            //病理/血液/影像检查报告
        }
            break;
        case 6:{
            //意外事故证明
        }
            break;
        default:
            break;
    }
    
    
    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选择", nil];
    [sheet showInView:self.view];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    ClaimPicTableViewCell *cell=(ClaimPicTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:self.tag]];
    UIImage *image=info[UIImagePickerControllerOriginalImage];
    [cell addImage:image];
    switch (self.tag) {
        case 0:{
            //医疗费用原始凭证(必填)
        }
            break;
        case 1:{
            //被保险人身份证明(必填)
        }
            break;
        case 2:{
            //门(急)诊病例(必填)
        }
            break;
        case 3:{
            //住院病例(出院小结)
        }
            break;
        case 4:{
            ///医疗费用明细清单/处方
        }
            break;
        case 5:{
            //病理/血液/影像检查报告
        }
            break;
        case 6:{
            //意外事故证明
        }
            break;
        default:
            break;
    }
}

#pragma mark - UIActionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            if(self.cameraPicker==nil){
                UIImagePickerController *cameraPicker=[[UIImagePickerController alloc]init];
                cameraPicker.sourceType=UIImagePickerControllerSourceTypeCamera;
                self.cameraPicker=cameraPicker;
                cameraPicker.delegate=self;
            }
            [self presentViewController:self.cameraPicker animated:YES completion:nil];
        }else{
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
        }
    }else if (buttonIndex==1){
        if(self.imagePicker==nil){
            UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
            imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            self.imagePicker=imagePicker;
            imagePicker.delegate=self;
        }
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }
    
}
@end
